package com.zrrd.learn.chapter08.reflex;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

/**
 * 机器方法 类
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/8/7 下午1:53
 */
public class RebotMethod {

    public RebotMethod() {
        System.out.println("ssss-->");
    }

    public int youTry(String x, int y) {
        return 123;
    }

    public static void main(String[] args) {

        //2 : 一个类的实例
        RebotMethod rm = new RebotMethod();
        //any RebotMethod 子类
        Class<? extends RebotMethod> class01 = rm.getClass();
        try {
            RebotMethod rm2 = class01.newInstance();

            Constructor<?>[] cs = class01.getConstructors();
            System.out.println(cs[0].getName());

            Object obj2 = cs[0].newInstance();

            //所有方法
            Method[] ms = class01.getDeclaredMethods();
            for (Method m : ms) {
                System.out.println("方法名称 ==> " + m.getName());

                System.out.println("你拿的方法有参数吗");

                System.out.println("参数个数" + m.getParameterCount());
                Parameter[] parameters = m.getParameters();

                for (Parameter p : parameters) {
                    System.out.println("参数名称：" + p.getName());
                    System.out.println("参数类型" + p.getType());
                }
                System.out.println("返回値类型" + m.getReturnType());

                if(m.getName().equals("youTry")){
                    //你执行这个方法 。我要用一下
                    Object obj = m.invoke(rm,"sss",23);
                    int n = (int) obj;
                    System.out.println(n);
                }

            }

        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }

        //3: 类的路径
        String path = "com.ftg.learn.chapter8.pattern.myreflech.RebotMethod";
        try {
            Class<?> class02 = Class.forName(path);
            Object obj = class02.newInstance();
            RebotMethod r1 = (RebotMethod) obj;
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

}